<template>
  <div>
    <el-form :model="form" label-width="auto" style="max-width: 600px; display: flex; flex-wrap: nowrap;">
      <el-form-item label="服务名称" style="width: 200px">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="服务类型" style="width: 200px">
        <el-select v-model="form.serveTypeId">
          <div v-for="item in serveTypeList" :key="item.id">
            <el-option :label="item.typename" :value="item.id" />
          </div>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" style="width: 200px">
        <el-select v-model="form.activeStatus">
          <el-option label="草稿" value="0" />
          <el-option label="禁用" value="1" />
          <el-option label="启用" value="2" />
        </el-select>
      </el-form-item>
      <el-button @click="findList()" type="primary">查询</el-button>
      <el-button @click="cleanForm()" type="primary">清空</el-button>
    </el-form>
    <el-button @click="dialogVisible = true" type="success">新增</el-button>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="code" label="服务编号" />
    <el-table-column prop="name" label="服务名称" />
    <el-table-column prop="serveItemIcon" label="服务图标">
      <template v-slot="scope">
        <img :src="scope.row.serveItemIcon" style="width: 100px;height: 100px" />
      </template>
    </el-table-column>
    <el-table-column prop="img" label="服务图片">
      <template v-slot="scope">
        <img :src="scope.row.img" style="width: 100px;height: 100px" />
      </template>
    </el-table-column>
    <el-table-column prop="referencePrice" label="服务单价（元）" />
    <el-table-column prop="typename" label="服务类型">
      <template v-slot="scope">
        {{scope.row.itemType.typename}}
      </template>
    </el-table-column>
    <el-table-column prop="description" label="服务描述" />
    <el-table-column prop="sortNum" label="排序" />
    <el-table-column prop="activeStatus" label="状态" >
      <template v-slot="scope">
        <el-tag v-if="scope.row.activeStatus==0" >草稿</el-tag>
        <el-tag v-if="scope.row.activeStatus==1" >禁用</el-tag>
        <el-tag v-if="scope.row.activeStatus==2" >启用</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="updateTime" label="更新时间" />
    <el-table-column prop="address" label="操作">
      <template v-slot="scope">
        <el-link type="danger" @click="deleteOne(scope.row.id)">删除</el-link>
        <el-link>&nbsp;</el-link>
        <el-link>&nbsp;</el-link>
        <el-link type="primary" @click="dialogVisible = true">编辑</el-link>
        <el-link>&nbsp;</el-link>
        <el-link>&nbsp;</el-link>
        <el-link v-if="scope.row.activeStatus==1 || scope.row.activeStatus==0" type="primary" @click="qiStatus(scope.row.id)">启用</el-link>
        <el-link v-if="scope.row.activeStatus==2" type="danger" @click="jinStatus(scope.row.id)">禁用</el-link>
      </template>
    </el-table-column>
  </el-table>
    <el-pagination
      v-model:current-page="currentPage4"
      v-model:page-size="pageSize4"
      :page-sizes="[1, 2, 3, 400]"
      :size="size"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog
      v-model="dialogVisible"
      title="新增服务"
      width="500"
    >
      <el-form :model="addForm" label-width="auto" style="max-width: 600px">
        <el-form-item label="服务名称:">
          <el-input v-model="addForm.name" />
        </el-form-item>
        <el-form-item label="服务类型">
          <el-select v-model="addForm.serveTypeId">
            <div v-for="item in serveTypeList" :key="item.id">
              <el-option :label="item.typename" :value="item.id" />
            </div>
          </el-select>
        </el-form-item>
        <el-form-item label="参考服务单价:">
          <el-input v-model="addForm.referencePrice" />
        </el-form-item>
        <el-form-item label="服务单位:">
          <el-input v-model="addForm.unit" />
        </el-form-item>
        <el-form-item label="服务描述:">
          <el-input v-model="form.description" type="textarea" />
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
const tableData = ref([])

const form=ref({
  pageNum:1,
  pageSize:4
})
const total=ref()
import {findAll,findAllType,qi,jin,deleteItem} from '@/api/item/item'
import {ElMessage} from "element-plus";
function findList(){
  findAll(form.value).then(res=>{
    tableData.value=res.data.data.records
    console.log(res.data.data.total)
    total.value=res.data.data.total
  })
}
findList()
const handleSizeChange = (val) => {
  form.value.pageSize=val
  findList()
}
const handleCurrentChange = (val) => {
  form.value.pageNum=val
  findList()
}
const serveTypeList=ref([])
function findTypeAll(){
  findAllType().then(res=>{
    console.log(res.data)
    serveTypeList.value=res.data.data
  })
}
findTypeAll()
function cleanForm(){
  form.value={}
}
const dialogVisible = ref(false)
function qiStatus(id){
  qi(id).then(arr =>{
    ElMessage.success("启用成功")
    findList()
  })
}
function jinStatus(id){
  jin(id).then(arr =>{
    ElMessage.error("禁用成功")
    findList()
  })
}
function deleteOne(id){
  deleteItem(id).then(arr =>{
    ElMessage.success("删除成功")
    findList()
  })
}
const addForm=ref({})
</script>
